<template>
	<el-dialog
		:title="editModel=='audit'?'加码申请审核':'加码申请'"
		:visible.sync="dialogVisible"
		width="50%"
		:before-close="handleClose"
		v-dialogDrag
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		class="barCode-dialog"
	>
		<el-form
			:model="ruleForm"
			ref="ruleForm"
			label-width="100px"
			class="demo-ruleForm"
			:rules="rules"
			:inline-message="true"
			v-loading="loading"
		>
			<el-row>
				<el-col :span="12">
					<el-form-item label="采购订单号">
						<el-input v-model="ruleForm.purOrdNo" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="物料描述">
						<el-input v-model="ruleForm.matName" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="订单数量">
						<el-input v-model="ruleForm.orderAmount" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="已加码数量">
						<el-input v-model="ruleForm.genCodeAmount" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="可加码数量">
						<el-input v-model="ruleForm.canGenAmount" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="类型">
						<el-radio-group v-model="ruleForm.type" :disabled="editModel!='add'" @change="codeTypeChange">
							<el-radio :label="1">大码</el-radio>
							<el-radio :label="2">小码</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="型号" prop="model">
						<el-input v-model="ruleForm.model" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="物料名称" prop="labMatName">
						<el-input v-model="ruleForm.labMatName" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="执行标准" prop="standard">
						<el-input v-model="ruleForm.standard" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="产地" prop="productionArea">
						<el-input v-model="ruleForm.productionArea" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="保质期" prop="shelfLife">
						<el-input v-model="ruleForm.shelfLife" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="净含量" prop="weight">
						<el-input v-model="ruleForm.weight" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="生产日期" prop="productDate">
						<el-date-picker
							v-model="ruleForm.productDate"
							type="date"
							value-format="yyyy-MM-dd"
							:disabled="editModel=='view'||editModel=='audit'"
						></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="生产批号" prop="batchCode">
						<el-input
							v-model="ruleForm.batchCode"
							maxlength="10"
							show-word-limit
							:disabled="editModel=='view'||editModel=='audit'"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="质检号" prop="inspectionCode">
						<el-input
							v-model="ruleForm.inspectionCode"
							maxlength="10"
							show-word-limit
							:disabled="editModel=='view'||editModel=='audit'"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="生成数量" prop="amount">
						<el-input
							v-model.number="ruleForm.amount"
							maxlength="10"
							@keyup.native="check"
							:disabled="editModel=='view'||editModel=='audit'"
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="原因" prop="remark">
						<el-input
							v-model="ruleForm.remark"
							maxlength="50"
							@keyup.native="check"
							type="textarea"
							show-word-limit
							:disabled="editModel=='view'||editModel=='audit'"
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="handleClose" v-if="editModel!='view'">取消</el-button>
			<el-button
				type="primary"
				:disabled="loading"
				@click="saveDataConfirm('save')"
				v-if="editModel=='edit'||editModel=='add'"
			>保存</el-button>
			<el-button
				type="primary"
				:disabled="loading"
				@click="saveDataConfirm('submit')"
				v-if="editModel=='edit'||editModel=='add'"
			>提交</el-button>
			<el-button
				type="primary"
				@click="saveDataConfirm('back')"
				v-if="editModel=='audit'"
				:disabled="loading"
			>退回</el-button>
			<el-button
				type="primary"
				@click="saveDataConfirm('audit')"
				v-if="editModel=='audit'"
				:disabled="loading"
			>审核</el-button>
		</span>
	</el-dialog>
</template>

<script>
import Msg from '@/public/message'
import subOperation from '@/public/addOperation'
export default {
	data () {
		return {
			rules: {
				model: [{
					required: true
				}],
				labMatName: [{
					required: true
				}],
				standard: [{
					required: true
				}],
				shelfLife: [{
					required: true
				}],
				productDate: [{
					required: true,
					trigger: "change",
					message: "请选择生产日期！"
				}],
				productionArea: [{
					required: true
				}],
				weight: [{
					required: true
				}],
				amount: [
					{
						required: true,
						trigger: "change",
						message: "生产数量不能为空！"
					},
					{ type: 'number', message: '生产数量必须为数字', trigger: "change", }
				],
				batchCode: [{
					required: true,
					trigger: "change",
					message: "生产批号不能为空！"
				}],
				inspectionCode: [{
					required: true,
					trigger: "change",
					message: "质检号不能为空！"
				}],
				remark: [{
					required: true,
					trigger: "change",
					message: "原因不能为空！"
				}]
			},
			disState: false,
			loading: false,
		}
	},
	props: {
		dialogVisible: {
			type: Boolean
		},
		editModel: {
			type: String,
			default: 'edit'
		},
		ruleForm: {
			type: Object
		},
		addedAmount: {
			type: Number
		}
	},
	methods: {
		// 关闭弹窗
		handleClose () {
			this.$refs['ruleForm'].resetFields()
			this.$emit('closeDialog')
		},
		// 保存或提交数据
		saveDataConfirm (type) {
			// const { canGenAmount, amount } = this.ruleForm

			// let canAmount = 0
			// if (this.editModel == 'edit') {
			// 	canAmount = this.addedAmount + (canGenAmount - 0)
			// } else {
			// 	canAmount = canGenAmount
			// }
			// if (canAmount <= 0) {
			// 	Msg.showPopMsg(`无可申请加码数量!`)
			// 	return
			// }
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					let pramas = {
						...this.ruleForm,
						productDate: new Date(this.ruleForm.productDate).Format("yyyy-MM-dd HH:mm:ss"),
						billState: type == "save" ? 0 : type == "submit" ? 1 : type == "audit" ? 3 : 2
					}
					this.loading = true
					if (this.editModel == 'add') {
						pramas.creatorId = this.$global.user.id
						pramas.creator = this.$global.user.employeeName
						if (type == "submit") {
							pramas.submitor = this.$global.user.employeeName
							pramas.submitorId = this.$global.user.id
						}
					} else if (this.editModel == 'edit') {
						pramas.modifyerId = this.$global.user.id
						pramas.modifyer = this.$global.user.employeeName
						if (type == "submit") {
							pramas.submitor = this.$global.user.employeeName
							pramas.submitorId = this.$global.user.id
						}
					} else if (this.editModel == "audit") {
						let obj = {
							id: pramas.id,
							billState: pramas.billState,
							modifyer: this.$global.user.employeeName,
							modifyerId: this.$global.user.id
						}
						this.$http.updateApplyBarcodeState(obj).then(({ data }) => {
							this.loading = false
							if (data.code == 1) {
								Msg.showSuccessMsg('操作成功')
								this.handleClose()
							} else {
								Msg.showErrorMsg(data.info)
							}
						}).catch(err => {
							this.loading = false
							Msg.showErrorMsg(data.info)
						})
						return
					}
					this.$http.saveBarcodeApply(pramas).then(({ data }) => {
						this.loading = false
						if (data.code == 1) {
							Msg.showSuccessMsg('保存成功')
							this.handleClose()
						} else {
							Msg.showErrorMsg(data.info)
						}
					}).catch(err => {
						this.loading = false
						Msg.showErrorMsg(err.data.message)
					})
				}
			})
		},
		// 校验数据格式
		check () {

		},
		codeTypeChange (val) {
			this.$emit('codeTypeChange', val)
		},
		// 重置表单
		resetForm () {
			this.$refs['ruleForm'].resetFields()
		}
	},
	components: {},
	beforeMount () {
	}
}
</script>

<style lang="less" scoped>
.barCode-dialog {
	.el-form {
		.el-form-item {
			margin-bottom: 5px;
		}
	}
}
</style>